using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BusinessLayer;

public partial class inventory_ManageItems : System.Web.UI.Page
{
    BL_UPC upcBL = new BL_UPC();
    BusinessLayer.BL_UPC.ItemStructure Item;
    BusinessLayer.BL_UPC.TypeStructure Type;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddlItemType.DataSource = upcBL.PopulateItemGroup();
            ddlItemType.DataTextField = "type";
            ddlItemType.DataValueField = "type_idx";
            ddlItemType.DataBind();
        }
        else
        {
        }
    }
    protected void ddlItemType_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Get the Text Value for the selected index and set that value to the textbox.
        Type = upcBL.RetrieveSpecificType(Convert.ToInt32(ddlItemType.SelectedValue));
        
        tbItemType.Text = Type.type;
        taDescItemType.Text = Type.description;
    }
    
    protected void btnAdd_Click(object sender, EventArgs e)//THIS ADDS Types!!!
    {
        if (upcBL.IsUniqueItemType(tbItemType.Text))
        {
            upcBL.AddItemType(tbItemType.Text, taDescItemType.Text);
            Response.Redirect("./ManageItems.aspx");
        }
        else
        {
            lblWarning.Visible = true;
            lblWarning.Text = "This ItemType already exists, please select a new name.";
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (ddlItemType.SelectedValue.Length > 0)
        {
            Type.index = Convert.ToInt32(ddlItemType.SelectedValue);
            Type.description = taDescItemType.Text;
            Type.type = tbItemType.Text;
            upcBL.UpdateItemType(Type);

            Response.Redirect("./ManageItems.aspx");
        }
        else
        {
            lblWarning.Visible = true;
            lblWarning.Text = "Please specify an Item Type to update.";
        }
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (ddlItemType.SelectedValue.Length > 0)
        {
            upcBL.DeleteItemType(Convert.ToInt32(ddlItemType.SelectedValue));
            Response.Redirect("./ManageItems.aspx");
        }
        else
        {
            lblWarning.Visible = true;
            lblWarning.Text = "Please specify an Item Type to delete.";
        }
    }

}
